﻿using JinShun.OA.UI.Apis;
using JinShun.OA.UI.Auth;
using JinShun.OA.UI.Dtos.Navigation;
using JinShun.OA.UI.Dtos.Post;
using JinShun.OA.UI.IServer.Post;
using JinShun.OA.UI.Models;
using JinShun.OA.UI.Urls;
using AutoMapper;
using System.Collections.Generic;

namespace JinShun.OA.UI.Services.Post
{
    public class PostService : IPost
    {
        private readonly TokenHelper tokenHelper;
        private readonly HttpWebClient http;
        public PostService(TokenHelper tokenHelper, HttpWebClient http)
        {
            this.tokenHelper = tokenHelper;
            this.http = http;
        }

        public async Task<List<PostModel>> GetPostsAsync()
        {
            List<PostModel> list = new List<PostModel>();
            var token = await tokenHelper.GetTokenAsync();
            PostGetListOutputDto um = await http.ExecuteAsync<PostGetListOutputDto>(UrlBases.Post, HttpMethod.Get, null, token);
            // 配置映射
            var config = new MapperConfiguration(cfg => {cfg.CreateMap<PostItems, PostModel>();});
            var mapper = config.CreateMapper();
            // 执行转换
            list = mapper.Map<List<PostItems>, List<PostModel>>(um.items);
            return list;
        }
    }
}
